package com.llyb.tingshu.payment.api;

import com.llyb.tingshu.common.login.TsLogin;
import com.llyb.tingshu.common.result.Result;
import com.llyb.tingshu.common.util.AuthContextHolder;
import com.llyb.tingshu.payment.service.PaymentInfoService;
import com.llyb.tingshu.payment.service.WxPayService;
import com.wechat.pay.java.service.payments.model.Transaction;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "微信支付接口")
@RestController
@RequestMapping("api/payment/wxPay")
@Slf4j
public class WxPayApiController {

    @Autowired
    private WxPayService wxPayService;

    @Autowired
    private PaymentInfoService paymentInfoService;


    @Operation(summary = "微信支付回调")
    @PostMapping("/notify")
    public Map<String, Object> wxNotify(HttpServletRequest  request){

        Map<String, Object> result = new HashMap<>();
        try {
            wxPayService.wxNotify(request);
            result.put("code", "SUCCESS");
            result.put("message", "成功");
            return result;
        } catch (Exception e) {
            log.error("微信支付回调异常：{}",e.getMessage());
        }
        result.put("code", "FAIL");
        result.put("message", "失败");
        return result;
    }

    /**
     * 创建微信支付二维码native 测试
     * @param paymentType
     * @param orderNo
     * @return
     */
    @TsLogin
    @Operation(summary = "微信Native下单")
    @Parameters({
            @Parameter(name = "paymentType",description = "支付类型：1301-订单 1302-充值",in = ParameterIn.PATH,required = true),
            @Parameter(name = "orderNo",description = "订单号",required = true,in = ParameterIn.PATH),
    })
    @PostMapping("/createNative/{paymentType}/{orderNo}")
    public Result<Map<String, Object>> createNative(@PathVariable String paymentType, @PathVariable String orderNo) {
        Map<String, Object> map = wxPayService.createNative(paymentType, orderNo, AuthContextHolder.getUserId());
        return Result.ok(map);
    }

    /**
     * 查询支付状态 https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_2.shtml
     * @param orderNo
     * @return
     */
    @Operation(summary = "支付状态查询")
    @GetMapping("/queryPayStatus/{orderNo}")
    public Result queryPayStatus(@PathVariable String orderNo) {
        //  调用服务层方法;
        Transaction result =wxPayService.queryPayStatus(orderNo);
        //  判断
        if (null != result && result.getTradeState().equals(Transaction.TradeStateEnum.SUCCESS)){
            //  支付成功;
            paymentInfoService.updatePaymentStatus(result);
            //  返回数据
            return Result.ok();
        }
        //  返回数据;
        return Result.fail();
    }


/*    *//**
     * 创建微信支付
     * @param paymentType
     * @param orderNo
     * @return
     *//*
    @TsLogin
    @Operation
    @PostMapping("/createJsapi/{paymentType}/{orderNo}")
    public Result createJsapi(@PathVariable String paymentType, @PathVariable String orderNo){

        Long userId = AuthContextHolder.getUserId();

        Map map = wxPayService.createJsapi(paymentType, orderNo,userId);
        return Result.ok(map);
    }*/


}
